package com.zle.common.util;

import javax.servlet.ServletContextEvent;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class SpringBeansUtil extends ContextLoaderListener {
	
	private static ApplicationContext applicationContext;
	//Log appLog = LogFactory.getLog("AppContextListener");
	
	public void contextInitialized(ServletContextEvent event) {

		super.contextInitialized(event);

		applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(event.getServletContext());
		 
	}
	@Override
	public void contextDestroyed(ServletContextEvent event) {
		 
		super.contextDestroyed(event);
	}
	/**
	 * 用于在其他类中获取bean
	 * @param beanName 首字母要小写
	 * @return
	 */
	public static Object getBean(String beanName) {
		
		return applicationContext.getBean(beanName);
	}
	/**
	 * 用于在当前类中获取bean
	 * @param <T>
	 * @param c
	 * @return
	 */
	public static <T> T getBean(Class<T> c){
		
		return applicationContext.getBean(c);
	}

}
